﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Util;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Reputation
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseReputationManager
    {
        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 给出脚本里面的可运动物体的数量(NPCs、Monster)
        /// </summary>
        public int Count
        {
            get { return m_Reputations.Count; }
        }
        #endregion

        #region zh-CHS ReputationManager方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, BaseReputation> m_Reputations = new SafeDictionary<long, BaseReputation>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseTaxiNode"></param>
        public bool AddReputation( long iReputationSerial, BaseReputation baseReputation )
        {
            if ( baseReputation == null )
            {
                Debug.WriteLine( "BaseReputationManager.AddReputation(...) - baseReputation == null error!" );
                return false;
            }

            EventHandler<BeforeAddReputationCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddReputationCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddReputationCallEventArgs eventArgs = new BeforeAddReputationCallEventArgs( baseReputation, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Reputations.Add( iReputationSerial, baseReputation );

            EventHandler<AfterAddReputationCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddReputationCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddReputationCallEventArgs eventArgs = new AfterAddReputationCallEventArgs( baseReputation, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool RemoveReputation( long iReputationSerial )
        {
            BaseReputation baseReputation = FindReputationOnSerial( iReputationSerial );
            if ( baseReputation == null )
                return false;

            EventHandler<BeforeRemoveReputationCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveReputationCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveReputationCallEventArgs eventArgs = new BeforeRemoveReputationCallEventArgs( baseReputation, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Reputations.Remove( iReputationSerial );

            EventHandler<AfterRemoveReputationCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveReputationCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveReputationCallEventArgs eventArgs = new AfterRemoveReputationCallEventArgs( baseReputation, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public BaseReputation FindReputationOnSerial( long iReputationSerial )
        {
            return m_Reputations.GetValue( iReputationSerial );
        }

        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarningAttribute( "(Reputation)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseReputation[] ToArray()
        {
            return m_Reputations.ToArray();
        }

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddReputationCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddReputationCallEventArgs> m_ThreadEventBeforeAddReputationCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddReputationCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddReputationCallEventArgs> ThreadBeforeAddReputationCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddReputationCall );
                {
                    m_ThreadEventBeforeAddReputationCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddReputationCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddReputationCall );
                {
                    m_ThreadEventBeforeAddReputationCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddReputationCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddReputationCallEventArgs> m_ThreadEventAfterAddReputationCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddReputationCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddReputationCallEventArgs> ThreadAfterAddReputationCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddReputationCall );
                {
                    m_ThreadEventAfterAddReputationCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddReputationCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddReputationCall );
                {
                    m_ThreadEventAfterAddReputationCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddReputationCall );
            }
        }
        #endregion

        #region zh-CHS RemoveReputationCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveReputationCallEventArgs> m_ThreadEventBeforeRemoveReputationCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveReputationCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveReputationCallEventArgs> ThreadBeforeRemoveReputationCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveReputationCall );
                {
                    m_ThreadEventBeforeRemoveReputationCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveReputationCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveReputationCall );
                {
                    m_ThreadEventBeforeRemoveReputationCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveReputationCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveReputationCallEventArgs> m_ThreadEventAfterRemoveReputationCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveReputationCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveReputationCallEventArgs> ThreadAfterRemoveReputationCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveReputationCall );
                {
                    m_ThreadEventAfterRemoveReputationCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveReputationCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveReputationCall );
                {
                    m_ThreadEventAfterRemoveReputationCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveReputationCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion